package cn.itsource.pethome.product.service.impl;

import cn.itsource.pethome.Basic.service.Impl.BaseServiceImpl;
import cn.itsource.pethome.Basic.util.PageList;
import cn.itsource.pethome.product.domain.Product;
import cn.itsource.pethome.product.domain.ProductDetail;
import cn.itsource.pethome.product.domain.dto.ProductDto;
import cn.itsource.pethome.product.mapper.ProductDetailMapper;
import cn.itsource.pethome.product.mapper.ProductMapper;
import cn.itsource.pethome.product.query.ProductQuery;
import cn.itsource.pethome.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public  class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;

    /**
     * 查询所有服务
     * @return
     */
    @Override
    @Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
    public List<Product> findAll() {
        return productMapper.findAll();
    }

    /**
     * 上架
     * @param ids
     */
    @Override
    public void onState(List<Long> ids) {
        productMapper.onSale(ids);
    }

    /**
     * 下架
     * @param ids
     */
    @Override
    public void downState(List<Long> ids) {
        productMapper.downState(ids);
    }

    /**
     * 增加服务
     * @param product
     */
    @Override
    public void addProduct(Product product) {
        //添加服务
        productMapper.add(product);
        //添加详情
        ProductDetail productDetail = product.getProductDetail();
        productDetail.setProduct(product);
        productDetailMapper.add(productDetail);
    }

    /**
     * 修改服务
     * @param product
     */
    @Override
    public void updateProduct(Product product) {
        productMapper.update(product);
        ProductDetail productDetail = product.getProductDetail();
        productDetail.setProduct(product);
        productDetailMapper.update(productDetail);
    }

    /**
     * 通过id查询
     * @param id
     * @return
     */
    @Override
    public Product findById(Long id) {
        return productMapper.findById(id);
    }
}
